package com.inossem.allotin.adapter.input;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.inossem.allotin.R;
import com.inossem.allotin.utils.DialogUtils;
import com.inossem.allotin.utils.Utils;
import com.inossem.wms.vo.transport.TransportHead;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static android.view.View.INVISIBLE;
import static android.view.View.VISIBLE;
import static com.inossem.allotin.constant.ItemStatusConstant.STATUS_CLOSED;
import static com.inossem.allotin.constant.ItemStatusConstant.STATUS_COMPLETED;
import static com.inossem.allotin.constant.ItemStatusConstant.STATUS_DELETED;
import static com.inossem.allotin.constant.ItemStatusConstant.STATUS_DRAFT;
import static com.inossem.allotin.constant.ItemStatusConstant.STATUS_ITEM_TWO;
import static com.inossem.allotin.constant.ItemStatusConstant.STATUS_ITEM_ZERO;
import static com.inossem.allotin.constant.ItemStatusConstant.STATUS_POSTING;
import static com.inossem.allotin.constant.ItemStatusConstant.STATUS_REJECTED;
import static com.inossem.allotin.constant.ItemStatusConstant.STATUS_SUBMITTED;
import static com.inossem.allotin.constant.ItemStatusConstant.STATUS_SYNC;
import static com.inossem.allotin.constant.ItemStatusConstant.STATUS_TASK;
import static com.inossem.allotin.constant.ItemStatusConstant.STATUS_UNSYNC;
import static com.inossem.allotin.constant.ItemStatusConstant.STATUS_WRITED_OFF;
import static com.inossem.allotin.constant.ItemStatusConstant.STATUS_WRITING_OFF;
import static com.inossem.allotin.constant.ItemStatusConstant.YEAR_MONTH_DAY;

/**
 * @author LinH
 */
public class TransportInputListAdapter extends RecyclerView.Adapter<TransportInputListAdapter.ViewHolder> {
    private List<TransportHead> list = new ArrayList<>();
    private Context context;
    private OnItemClickListener listenerItem = null;
    private OnDeleteClickListener listenerDelete = null;

    public void addList(List<TransportHead> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    public void addAllList(List<TransportHead> list) {
        this.list.addAll(list);
        notifyDataSetChanged();
    }

    public List<TransportHead> getList() {
        return this.list;
    }

    public void adapterClear() {
        this.list.clear();
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
        context = viewGroup.getContext();
        return new ViewHolder(LayoutInflater.from(context).inflate(R.layout.item_transport_input_list, viewGroup, false));
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        //根据position改变item背景
        if (position % STATUS_ITEM_TWO == STATUS_ITEM_ZERO) {
            holder.layout.setBackgroundColor(ContextCompat.getColor(context, R.color.base_white));
        } else {
            holder.layout.setBackgroundColor(ContextCompat.getColor(context, R.color.base_bg));
        }
        // 验收单号
        holder.tv1.setText(list.get(position).getStockTransportCode());
        // 申请单号
        holder.tv2.setText(list.get(position).getCusApplyCode());
        // 供应商描述
        holder.tv4.setText(list.get(position).getRemark());
        // 创建日期
        Date createTime = list.get(position).getCreateTime();
        String s = new SimpleDateFormat(YEAR_MONTH_DAY).format(createTime);
        holder.tv5.setText(s);
        // 创建人
        holder.tv6.setText(list.get(position).getCreateUserName());
        // 单据状态名称
//        holder.tv7.setText(list.get(position).getReceiptStatusName());
        if (list.get(position).isEdit()) {
            holder.tv7.setText("已保存");
        } else {
            holder.tv7.setText("未保存");
        }
        // 是否显示删除
        Byte i = list.get(position).getReceiptStatus();
        // 根据ReceiptStatus更改statusTv颜色,ivDelete是否显示
        if (i == STATUS_DRAFT) {
            holder.tv7.setTextColor(ContextCompat.getColor(context, R.color.base_green));
            holder.iv.setVisibility(VISIBLE);
        } else if (i == STATUS_SUBMITTED) {
            holder.tv7.setTextColor(ContextCompat.getColor(context, R.color.title_gray));
            holder.iv.setVisibility(INVISIBLE);
        } else if (i == STATUS_POSTING) {
            holder.tv7.setTextColor(ContextCompat.getColor(context, R.color.title_gray));
            holder.iv.setVisibility(INVISIBLE);
        } else if (i == STATUS_TASK) {
            holder.tv7.setTextColor(ContextCompat.getColor(context, R.color.base_prasinous));
            holder.iv.setVisibility(INVISIBLE);
        } else if (i == STATUS_UNSYNC) {
            holder.tv7.setTextColor(ContextCompat.getColor(context, R.color.base_orange));
            holder.iv.setVisibility(INVISIBLE);
        } else if (i == STATUS_SYNC) {
            holder.tv7.setTextColor(ContextCompat.getColor(context, R.color.title_gray));
            holder.iv.setVisibility(INVISIBLE);
        } else if (i == STATUS_COMPLETED) {
            holder.tv7.setTextColor(ContextCompat.getColor(context, R.color.title_gray));
            holder.iv.setVisibility(INVISIBLE);
        } else if (i == STATUS_WRITING_OFF) {
            holder.tv7.setTextColor(ContextCompat.getColor(context, R.color.title_gray));
            holder.iv.setVisibility(INVISIBLE);
        } else if (i == STATUS_WRITED_OFF) {
            holder.tv7.setTextColor(ContextCompat.getColor(context, R.color.title_gray));
            holder.iv.setVisibility(INVISIBLE);
        } else if (i == STATUS_DELETED) {
            holder.tv7.setTextColor(ContextCompat.getColor(context, R.color.title_gray));
            holder.iv.setVisibility(INVISIBLE);
        } else if (i == STATUS_CLOSED) {
            holder.tv7.setTextColor(ContextCompat.getColor(context, R.color.title_gray));
            holder.iv.setVisibility(INVISIBLE);
        } else if (i == STATUS_REJECTED) {
            holder.tv7.setTextColor(ContextCompat.getColor(context, R.color.text_red));
            holder.iv.setVisibility(VISIBLE);
        }
        holder.iv.setVisibility(View.GONE);
        // 是否删除Dialog
        holder.iv.setOnClickListener(v -> DialogUtils.getMsgDialog(context, () -> {
            listenerDelete.onButtonClick(position);
            list.remove(position);
            notifyDataSetChanged();
        }).show());
        // 点击事件
        holder.layout.setOnClickListener(v -> listenerItem.onButtonClick(position));
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public void setOnItemClickListener(OnItemClickListener l) {
        this.listenerItem = l;
    }

    public void setOnDeleteClickListener(OnDeleteClickListener l) {
        this.listenerDelete = l;
    }

    public interface OnItemClickListener {
        /**
         * Item回调
         *
         * @param s position
         */
        void onButtonClick(int s);
    }

    public interface OnDeleteClickListener {
        /**
         * Delete回调
         *
         * @param s position
         */
        void onButtonClick(int s);
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        TextView tv1;
        TextView tv2;
        TextView tv4;
        TextView tv5;
        TextView tv6;
        TextView tv7;
        ImageView iv;
        LinearLayout layout;

        ViewHolder(View view) {
            super(view);
            Utils.setViewLine(itemView);
            tv1 = view.findViewById(R.id.tv_1);
            tv2 = view.findViewById(R.id.tv_2);
            tv4 = view.findViewById(R.id.tv_4);
            tv5 = view.findViewById(R.id.tv_5);
            tv6 = view.findViewById(R.id.tv_6);
            tv7 = view.findViewById(R.id.tv_7);
            iv = view.findViewById(R.id.iv);
            layout = view.findViewById(R.id.layout);
        }
    }
}